const source = {
  carbonInventoryId: null,
  totalCarbonEmission: null,
  carbonEmission: null,
  emissionReduction: null,
  status: null,
  dataList: [
    {
      id: 1,
      name: '直接温室气体排放和移除',
      xh: '类别1',
      categoryType: 0,
      data: null,
      ratio: null,
      range: null,
      categoryList: [
        {
          id: 7,
          name: '固定燃烧源',
          xh: '类别1.1',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 8,
          name: '移动燃烧源',
          xh: '类别1.2',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 9,
          name: '工业过程排放和清除',
          xh: '类别1.3',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 10,
          name: '逸散排放',
          xh: '类别1.4',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 11,
          name: '土地利用、土地利用变化和林业产生的排放和清除',
          xh: '类别1.5',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
      ],
    },
    {
      id: 2,
      name: '输入能源的间接温室气体排放',
      xh: '类别2',
      categoryType: 0,
      data: null,
      ratio: null,
      range: null,
      categoryList: [
        {
          id: 12,
          name: '电力',
          xh: '类别2.1',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 13,
          name: '蒸汽',
          xh: '类别2.2',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 14,
          name: '热力',
          xh: '类别2.3',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 15,
          name: '冷量',
          xh: '类别2.4',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 16,
          name: '压缩空气',
          xh: '类别2.5',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
      ],
    },
    {
      id: 3,
      name: '运输产生的间接温室气体排放',
      xh: '类别3',
      categoryType: 0,
      data: null,
      ratio: null,
      range: null,
      categoryList: [
        {
          id: 17,
          name: '货物上游运输和配送',
          xh: '类别3.1',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 18,
          name: '货物下游运输和配送',
          xh: '类别3.2',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 19,
          name: '员工通勤',
          xh: '类别3.3',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 20,
          name: '客户和访客交通',
          xh: '类别3.4',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 21,
          name: '商务差旅',
          xh: '类别3.5',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
      ],
    },
    {
      id: 4,
      name: '组织使用产品和服务产生的间接温室气体排放',
      xh: '类别4',
      categoryType: 0,
      data: null,
      ratio: null,
      range: null,
      categoryList: [
        {
          id: 22,
          name: '购买的货物',
          xh: '类别4.1',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 23,
          name: '资本货物',
          xh: '类别4.2',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 24,
          name: '固体和液体废物处置',
          xh: '类别4.3',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 25,
          name: '租赁资产的使用',
          xh: '类别4.4',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 26,
          name: '其他服务',
          xh: '类别4.5',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
      ],
    },
    {
      id: 5,
      name: '与使用组织产品有关的间接温室气体排放',
      xh: '类别5',
      categoryType: 0,
      data: null,
      ratio: null,
      range: null,
      categoryList: [
        {
          id: 27,
          name: '产品使用阶段的排放或清除',
          xh: '类别5.1',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 28,
          name: '下游租赁资产',
          xh: '类别5.2',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 29,
          name: '产品生命周期结束阶段',
          xh: '类别5.3',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
        {
          id: 30,
          name: '投资排放',
          xh: '类别5.4',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
      ],
    },
    {
      id: 6,
      name: '其他间接温室气体排放',
      xh: '类别6',
      categoryType: 0,
      data: null,
      ratio: null,
      range: null,
      categoryList: [
        {
          id: 31,
          name: '其他间接排放',
          xh: '类别6.1',
          categoryType: 0,
          data: null,
          ratio: null,
          facilityActivityList: [],
        },
      ],
    },
  ],
}

const activity = {
  carbonInventoryId: 8,
  categoryId: 51,
  name: '活动名称',
  emissionSource: '电力',
  emissionType: 0,
  sourceType: 1,
  entryMethod: 0,
  indicatorNumber: 'EU02.ELETPF',
  unit: '千克二氧化碳',
  yearNum: 0,
  dataExplain: '数据说明',
  gasExplain: '因子说明',
  pointId: 2,
  protocolCode: 'ISO 14064-1 : 2018',
  dataTime: '2025',
  gasList: [
    { gas: 'CO2', emissionFactor: 0.5752, emissionUnitFactor: 'kgCO2e/千瓦时', emissionUnit: 'kgCO2', gwp: 1, discountValue: 1 },
    { gas: 'NF3', emissionFactor: 1, emissionUnitFactor: 'kgCO2e/千瓦时', emissionUnit: 'kgNF3', gwp: 17400, discountValue: 1 },
    { gas: 'CH4', emissionFactor: 1, emissionUnitFactor: 'kgCO2e/千瓦时', emissionUnit: 'kgCH4', gwp: 27.9, discountValue: 1 },
    { gas: 'PFCs', emissionFactor: 1, emissionUnitFactor: 'kgCO2e/千瓦时', emissionUnit: 'kgPFCs', gwp: 111, discountValue: 1 },
    { gas: 'N2O', emissionFactor: 1, emissionUnitFactor: 'kgCO2e/千瓦时', emissionUnit: 'kgN2O', gwp: 273, discountValue: 1 },
    { gas: 'HFCs', emissionFactor: 1, emissionUnitFactor: 'kgCO2e/千瓦时', emissionUnit: 'kgHFCs', gwp: 100, discountValue: 1 },
    { gas: 'SF6', emissionFactor: 1, emissionUnitFactor: 'kgCO2e/千瓦时', emissionUnit: 'kgSF6', gwp: 25200, discountValue: 1 },
  ],
}

const subCategoryIds: number[] = []

for (const item of source.dataList) {
  for (const child of item.categoryList) {
    subCategoryIds.push(child.id)
  }
}

function generateActivity(categoryId: number) {
  return {
    ...activity,
    categoryId,
    name: `${activity.name} ${categoryId}`,
    dataExplain: `${activity.dataExplain} ${categoryId}`,
    gasExplain: `${activity.gasExplain} ${categoryId}`,
  }
}

async function saveActivity(activity: any) {
  return carbonInventoryApi.saveOrUpdateCarbonInventoryActivity(activity)
}

export const max = subCategoryIds.length

export async function start(start: number, end: number) {
  for (let i = start; i < end; i++) {
    const categoryId = subCategoryIds[i]
    if (categoryId) {
      const activity = generateActivity(categoryId)
      console.log(Date.now(), categoryId)
      await saveActivity(activity)
      console.log(Date.now(), categoryId)
    }
  }
}
